package com.damai.controller;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.citywithincity.api.ApiFilter;
import com.citywithincity.api.utils.ApiUtil;
import com.citywithincity.db.Db;
import com.citywithincity.db.Record;
import com.citywithincity.db.drivers.mysql.MysqlSqlHelper;
import com.citywithincity.db.meta.ColumnStruct;
import com.citywithincity.db.meta.DbStructFactory;
import com.citywithincity.db.meta.TableStruct;
import com.citywithincity.db.meta.TableStructFilter;
import com.damai.action.factories.SimpleJsonApiFactory;
import com.damai.action.factories.WebJsonActionFactory;
import com.damai.action.meta.ApiMetaData;
import com.damai.annotation.Action;
import com.damai.annotation.Controller;
import com.damai.annotation.Crypt;
import com.damai.annotation.Model;
import com.damai.controller.entities.TableStructVo;
import com.damai.error.ApiError;
import com.damai.excel.ExcelParseError;

@Controller(key="structs_api")
@Action(value = SimpleJsonApiFactory.class)
public class StructsApi {

	
	public Collection<TableStruct> structs(Integer index){
		return Db.getStruct(index).getTableStructs(DbStructFactory.STRUCT_USING);
	}
	@Crypt
	public ApiMetaData api(String api){
		return ApiFilter.me().getApi(api);
	}
	
	public List<ApiMetaData> apis(){
		return  ApiUtil.getUsingApi();
	}
	
	public List<ApiMetaData> module(String module){
		if(!module.startsWith("/")){
			module = "/" + module;
		}
		
		List<ApiMetaData> result = new ArrayList<ApiMetaData>();
		for (ApiMetaData data : ApiUtil.getUsingApi()) {
			if(data.getName().startsWith(module)){
				result.add(data);
			}
		}
		return result;
	}
	
	public List<String> tableQuery(final String key){
		Collection<TableStruct> structs= Db.getStruct(0).query(new TableStructFilter() {
			@Override
			public boolean accept(TableStruct struct) {
				return struct.getName().startsWith(key);
			}
		});
		
		List<String> names = new ArrayList<String>();
		for (TableStruct struct : structs) {
			names.add(struct.getName());
		}
		return names;
	}
	
	public TableStructVo table(String table,Integer index){
		if(index == null){
			index = 0;
		}
		TableStruct struct= Db.getStruct(index).getTableStruct(table);
		if(struct==null){
			throw new ApiError("找不到");
		}
		TableStructVo result = new TableStructVo();
		result.setColumns(struct.getColumns());
		result.setComment(struct.getComment());
		result.setName(struct.getName());
		
		//找到主键
		ColumnStruct[] columnStructs = struct.getPks();
		if(columnStructs!=null && columnStructs.length >0){
			if(columnStructs.length == 1){
				result.setPk(columnStructs[0].getName());
			}else{
				
			}
		}
	
		return result;
	}
	
	@Model
	private StructsModel model;
	
	@Action(WebJsonActionFactory.class)
	public Map<String, List<ColumnStruct>> exportDiff(HttpServletRequest request) throws ExcelParseError, IOException{
		return model.exportDiff();
	}
	
	@Action(WebJsonActionFactory.class)
	public Set<String> exportDiffTables(HttpServletRequest request) throws ExcelParseError, IOException{
		return model.exportDiff().keySet();
	}
	
	@Action(WebJsonActionFactory.class)
	public Set<String> tableAlter(HttpServletRequest request) throws ExcelParseError, IOException{
		
		List<Record> list = Db.ar().query( MysqlSqlHelper.allTableNames("ebusiness")  );
		
		
		
		for (Record record : list) {
			System.out.println("rename table "+record.getString("table_name")+" to "+ record.getString("table_name").toLowerCase()+";");
		}
		
		return null;
		
	}
}
